package com.dtt.company.department.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.dtt.company.department.model.DepartmentModel;
import com.dtt.company.department.service.DepartmentService;
import com.dtt.company.util.FmtRequest;

@WebServlet("/department")
public class DepartmentServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	private DepartmentService service=new DepartmentService();
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action=req.getParameter("action");
		String result="";
		switch(action) {
			case "add":
			    result= add(req);
				break;
			case "sel":
				result=sel(req);
				break;
			case "del":
				result=del(req);
				break;
			case "get":
				result=get(req);
				break;
			case "upd":
				result=upd(req);
				break;
			case "list":
				result=list(req);
				break;
			default:
				break;
		}
		FmtRequest.out(resp, result);		
	}
	
	//分页
	private String list(HttpServletRequest req) {
		String code=req.getParameter("code");
		String name=req.getParameter("name");
		String pageIndex=req.getParameter("pageIndex");
		String pageLimit=req.getParameter("pageLimit");
		DepartmentModel model=new DepartmentModel(code, name);
		model.setPageIndex(Integer.parseInt(pageIndex));//将页面传来的转换为int类型
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		
		List<DepartmentModel> list = service.select(model);
//		return new JSONArray(list).toString();
		int count=service.selectCount(model);
		Map<String,Object> map=new HashMap<>();
		map.put("data", list);
		map.put("count",count);
		return  new JSONObject(map).toString();
		
	}
	private String upd(HttpServletRequest req) {
		String code=req.getParameter("code");
		String name=req.getParameter("name");
		String tel=req.getParameter("tel");
		DepartmentModel model=new DepartmentModel(code, name, tel);
		return service.update(model);
	}
	private String get(HttpServletRequest req) {
		String code=req.getParameter("code");
		DepartmentModel model = new DepartmentModel(code);
		DepartmentModel mdb = service.selectModel(model);
		return new JSONObject(mdb).toString();
	}
	private String del(HttpServletRequest req) {
		String code=req.getParameter("code");
		DepartmentModel model = new DepartmentModel(code);
		return service.delete(model);
	}
	private String sel(HttpServletRequest req) {
		String code=req.getParameter("code");
		String name=req.getParameter("name");
		DepartmentModel model=new DepartmentModel(code, name);
		List<DepartmentModel> list = service.select(model);
		return new JSONArray(list).toString();//json 数组格式的字符串
	}
	
	private String add(HttpServletRequest req) {
		String code=req.getParameter("code");
		String name=req.getParameter("name");
		String tel=req.getParameter("tel");
		DepartmentModel model=new DepartmentModel(code, name, tel);
		
		return service.insert(model);
	}
	
	
}
